<template>
  <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
            <el-form-item label="创建时间">
                <el-date-picker
                  v-model="dateRange"
                  style="width: 240px"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
        <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
		<el-button
		  icon="el-icon-plus"
		  @click="$tools.clearPageCacheTo(`/products/productAdd`)"
		  size="mini"
		  plain
		  type="primary">新增产品</el-button>
		<el-button
		  type="warning"
		  plain
		  icon="el-icon-download"
		  size="mini"
		  @click="handleExport"
		  v-hasPermi="['system:role:export']">导出</el-button>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      :cell-style="{ 'text-align': 'center' }"
      :header-cell-style="{ 'text-align': 'center' }"
      @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="商品名" prop="productName" width="120" />
        <el-table-column label="创建日期" prop="createTime" width="150" :formatter="row => parseTime(row.createTime, `{y}-{m}-{d}`)"/>
        <el-table-column label="品牌名" prop="brandName"/>
        <el-table-column label="类型" prop="type">
            <template #default="{ row }">
                <dict-tag :options="dict.type.com_type" :value="row.type"/>
            </template>
        </el-table-column>
        <el-table-column label="更新时间" prop="updateTime" :formatter="row => parseTime(row.updateTime, `{y}-{m}-{d}`)"/>
        <el-table-column label="状态" prop="isEnable"/>
        <el-table-column label="备注信息" prop="remark"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope" v-if="scope.row.roleId !== 1">
            <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:role:edit']"
            >修改</el-button>
            <el-button
                size="mini"
                type="text"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:role:remove']"
            >删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listProduct, delProduct} from "@/api/products/productList";
export default {
    name: "Product",
    dicts: ['com_type'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 日期范围
            dateRange: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                roleName: undefined,
                roleKey: undefined,
                useFlag: undefined
            },
            list: [],
        };
    },
    created() {
        this.getList();
    },
	methods: {
		/** 查询角色列表 */
		getList() {
			this.loading = true;
			listProduct(this.addDateRange(this.queryParams, this.dateRange)).then(({ data }) => {
				this.list = data.records;
				this.total = data.total;
				this.loading = false;
			})
		},
		// 表单重置
		reset() {
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.dateRange = [];
			this.resetForm("queryForm");
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.roleId)
			this.single = selection.length!=1
			this.multiple = !selection.length
		},
        handleUpdate(row){
            this.$tools.clearPageCacheTo(`/products/productAdd?id=${row.id}`)
        },
		/** 删除按钮操作 */
		handleDelete(row) {
			const roleIds = row.roleId || this.ids;
			this.$modal.confirm('是否确认删除角色编号为"' + roleIds + '"的数据项？').then(function() {
				return delProduct(roleIds);
			}).then(() => {
				this.getList();
				this.$modal.msgSuccess("删除成功");
			}).catch(() => {});
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download('sysRole/export', {
				...this.queryParams
			}, `role_${new Date().getTime()}.xlsx`)
		}
    }
};
</script>
